package com.lzh.others;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;

import java.math.BigDecimal;

/**
 * 布隆过滤器
 */
public class BloomFilterCase {

    /**
     * 函数
     * 大小
     * 误判率
     */
    private static BloomFilter<Integer> bloomFilter = BloomFilter.create(Funnels.integerFunnel(), 1000000, 0.01);

    private static int TOTAL = 1000000;

    public static void main(String[] args) {
        for (int i = 0; i < TOTAL; i++) {
            bloomFilter.put(i);
        }

        //用另外的10万数据验证误判率
        int count = 0;
        for (int i = TOTAL; i < TOTAL + 100000; i++) {
            if (bloomFilter.mightContain(i)) {
                count++;
                System.out.println("误判了：i = " + i);
            }
        }

        System.out.println("误判个数：" + count);
        System.out.println("误判率：" + new BigDecimal(count).divide(new BigDecimal(TOTAL), 2, BigDecimal.ROUND_HALF_UP).doubleValue() + "%");
    }
}
